#include "Broker.h"
#include <zmq.h>
#include <assert.h>


const char* Broker::XpubAddr = "tcp://127.0.0.1:8000";
const char* Broker::XsubAddr = "tcp://127.0.0.1:8001";

void* Broker::Context()
{
  static void* ctx=zmq_init(1);
  return ctx;
}

Broker::Broker():xpub_(0),xsub_(0)
{
  xpub_ = zmq_socket(Context(), ZMQ_XPUB);
  xsub_ = zmq_socket(Context(), ZMQ_XSUB);

  const int rc1 = zmq_bind(xpub_, XpubAddr);
  assert(rc1 == 0);

  const int rc2 = zmq_bind(xsub_, XsubAddr);
  assert(rc2 == 0);

  int verbose=1;
  const int r3=zmq_setsockopt(xpub_,ZMQ_XPUB_VERBOSE, &verbose, sizeof(verbose));
  assert(r3 == 0);
}

Broker::~Broker()
{
}

void Broker::run(void)
{
  printf("(%s:%d) running broker\n",__FILE__,__LINE__);
  zmq_proxy(xsub_,xpub_,0);
  printf("(%s:%d) done running broker\n",__FILE__,__LINE__);
}
